/*
 * Created by JFormDesigner on Mon Apr 26 22:24:31 CST 2021
 */

package com.lgz.dbcourse.gui;

import com.lgz.dbcourse.dto.*;
import com.lgz.dbcourse.entity.MovieSeatEntity;
import com.lgz.dbcourse.service.UserService;
import com.lgz.dbcourse.service.impl.UserServiceImpl;
import com.lgz.dbcourse.util.CommonUtil;

import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
public class UF extends MyFrame {
    private UserService userService;
    private ScheduleDTO[] scheduleDTOS;
    private AudienceDTO audienceDTO;
    private MovieSeatDTO[] selectSeats;
    private MovieSeatDTO[] movieSeatDTOS;

    public void start() {
        init();
        this.setVisible(true);
    }

    public void init() {
//        initList1(userService.getSchedule());
        initTable(userService.getSchedule());
    }

    public UF(String phone) {
        userService = new UserServiceImpl();
        initComponents();
        this.audienceDTO = (AudienceDTO)userService.login(phone).getData();
//        System.out.println(this.audienceDTO.getAudienceDisplay());
    }

    public void initTable(ResultSet resultSet) {
        scheduleDTOS = (ScheduleDTO[])resultSet.getData();
        String[] columns = {"影片", "影厅", "开始时间"};
        String[][] data = new String[scheduleDTOS.length][3];
        for (int i = 0; i < scheduleDTOS.length; i++) {
            data[i][0] = scheduleDTOS[i].getMovieName();
            data[i][1] = scheduleDTOS[i].getHallName();
            data[i][2] = scheduleDTOS[i].getStartTime();
        }
        DefaultTableModel model=new DefaultTableModel(data, columns);
        this.table1.setModel(model);
    }

//    public void initList1(ResultSet resultSet) {
//        scheduleDTOS = (ScheduleDTO[])resultSet.getData();
//        String[] data = new String[scheduleDTOS.length];
//        for (int i = 0; i < scheduleDTOS.length; i++) {
//            data[i] = scheduleDTOS[i].toString();
//        }
//        this.list1.setListData(data);
//    }

    private void initList2() {
        try {
            movieSeatDTOS = (MovieSeatDTO[])userService.getSeat(CommonUtil.getMapperFactory().getMapperFacade().map(scheduleDTOS[this.table1.getSelectedRow()], MovieSeatDTO.class)).getData();
        } catch (ArrayIndexOutOfBoundsException e) {
            return;
        }
        String[] data = new String[movieSeatDTOS.length];
        for (int i = 0; i < movieSeatDTOS.length; i++) {
            data[i] = movieSeatDTOS[i].getSeatCode();
        }
        this.list2.setListData(data);
    }

    private void list1ValueChanged(ListSelectionEvent e) {
        // TODO add your code here


    }

    private void list2ValueChanged(ListSelectionEvent e) {
        // TODO add your code here
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        int[] index = this.list2.getSelectedIndices();
        selectSeats = new MovieSeatDTO[index.length];
        for (int i = 0; i < index.length; i ++) {
            selectSeats[i] = movieSeatDTOS[index[i]];
            selectSeats[i].setAudienceId(audienceDTO.getAudienceId());
        }
        userService.selectSeat(selectSeats);
        initList2();
        new Error(this, "选购成功").start();
    }

    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
        new Me(this, audienceDTO).start();
    }

//    private void list1MouseClicked(MouseEvent e) {
//        // TODO add your code here
//        initList2();
//    }

    private void list2MouseClicked(MouseEvent e) {
        // TODO add your code here

    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here
        initList2();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
        panel5 = new JPanel();
        panel6 = new JPanel();
        panel8 = new JPanel();
        panel9 = new JPanel();
        panel10 = new JPanel();
        panel11 = new JPanel();
        label1 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel7 = new JPanel();
        panel12 = new JPanel();
        panel13 = new JPanel();
        panel14 = new JPanel();
        scrollPane2 = new JScrollPane();
        list2 = new JList<>();
        panel15 = new JPanel();
        panel16 = new JPanel();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        setMinimumSize(new Dimension(720, 490));
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(30, 0));
            panel1.setLayout(new BorderLayout());
        }
        contentPane.add(panel1, BorderLayout.WEST);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(0, 30));
            panel2.setLayout(new BorderLayout());
        }
        contentPane.add(panel2, BorderLayout.NORTH);

        //======== panel3 ========
        {
            panel3.setPreferredSize(new Dimension(0, 30));
            panel3.setLayout(new BorderLayout());
        }
        contentPane.add(panel3, BorderLayout.SOUTH);

        //======== panel4 ========
        {
            panel4.setPreferredSize(new Dimension(30, 0));
            panel4.setLayout(new BorderLayout());
        }
        contentPane.add(panel4, BorderLayout.EAST);

        //======== panel5 ========
        {
            panel5.setLayout(new GridLayout(2, 1));

            //======== panel6 ========
            {
                panel6.setLayout(new BorderLayout());

                //======== panel8 ========
                {
                    panel8.setLayout(new BorderLayout());
                }
                panel6.add(panel8, BorderLayout.WEST);

                //======== panel9 ========
                {
                    panel9.setLayout(new BorderLayout());
                }
                panel6.add(panel9, BorderLayout.NORTH);

                //======== panel10 ========
                {
                    panel10.setLayout(new BorderLayout());
                }
                panel6.add(panel10, BorderLayout.SOUTH);

                //======== panel11 ========
                {
                    panel11.setLayout(new BorderLayout());

                    //---- label1 ----
                    label1.setText("\u6b63\u5728\u4e0a\u6620");
                    label1.setPreferredSize(new Dimension(70, 17));
                    label1.setHorizontalAlignment(SwingConstants.CENTER);
                    label1.setMaximumSize(new Dimension(70, 17));
                    label1.setMinimumSize(new Dimension(70, 17));
                    panel11.add(label1, BorderLayout.WEST);
                }
                panel6.add(panel11, BorderLayout.EAST);

                //======== scrollPane1 ========
                {
                    scrollPane1.setPreferredSize(new Dimension(452, 156));

                    //---- table1 ----
                    table1.setPreferredSize(new Dimension(150, 59));
                    table1.setMinimumSize(new Dimension(30, 40));
                    table1.setMaximumSize(new Dimension(12, 40));
                    table1.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            table1MouseClicked(e);
                        }
                    });
                    scrollPane1.setViewportView(table1);
                }
                panel6.add(scrollPane1, BorderLayout.CENTER);
            }
            panel5.add(panel6);

            //======== panel7 ========
            {
                panel7.setLayout(new BorderLayout());

                //======== panel12 ========
                {
                    panel12.setLayout(new BorderLayout());
                }
                panel7.add(panel12, BorderLayout.NORTH);

                //======== panel13 ========
                {
                    panel13.setLayout(new BorderLayout());
                }
                panel7.add(panel13, BorderLayout.WEST);

                //======== panel14 ========
                {
                    panel14.setLayout(new BorderLayout());
                }
                panel7.add(panel14, BorderLayout.SOUTH);

                //======== scrollPane2 ========
                {

                    //---- list2 ----
                    list2.addListSelectionListener(e -> list2ValueChanged(e));
                    list2.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            list2MouseClicked(e);
                        }
                    });
                    scrollPane2.setViewportView(list2);
                }
                panel7.add(scrollPane2, BorderLayout.CENTER);

                //======== panel15 ========
                {
                    panel15.setLayout(new GridLayout(3, 1));

                    //======== panel16 ========
                    {
                        panel16.setPreferredSize(new Dimension(70, 0));
                        panel16.setLayout(new GridLayout());
                    }
                    panel15.add(panel16);

                    //---- button1 ----
                    button1.setText("\u9009\u8d2d");
                    button1.addActionListener(e -> button1ActionPerformed(e));
                    panel15.add(button1);

                    //---- button2 ----
                    button2.setText("\u6211\u7684");
                    button2.addActionListener(e -> button2ActionPerformed(e));
                    panel15.add(button2);
                }
                panel7.add(panel15, BorderLayout.EAST);
            }
            panel5.add(panel7);
        }
        contentPane.add(panel5, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JPanel panel6;
    private JPanel panel8;
    private JPanel panel9;
    private JPanel panel10;
    private JPanel panel11;
    private JLabel label1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel7;
    private JPanel panel12;
    private JPanel panel13;
    private JPanel panel14;
    private JScrollPane scrollPane2;
    private JList<String> list2;
    private JPanel panel15;
    private JPanel panel16;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
